<?php
/**
 * 投资管理模块
 */
namespace Admin\Controller\Invest;
use Think\Controller;

class InvestController extends Controller 
{
	/**
	 *	管理后台菜单初始化与用户登录身份验证
	 */
	public function _initialize() 
	{
		// 判断用户是否登陆
		if (1!=1) //	!session('?regist_id')
		{
			$url = 'http://' . $_SERVER['HTTP_HOST'] .'/tp_bypal/index.php';
			$this->redirect($url);
			// echo "<script>alert('权限不足，无法操作');parent.location.href='".$url."';</script>";
			exit;
		}
	}


	/**
	 * 投资管理列表页
	 */
	public function invest_manage() 
	{
		$numSelect = I("post.numSelect","");
		$typeSelect = I("post.typeSelect","");
		$markSelect = I("post.markSelect","");

		$search_array['numSelect'] = $numSelect;
		$search_array['typeSelect'] = $typeSelect;
		$search_array['markSelect'] = $markSelect;
		$this->assign("search_array",$search_array);

		$invest = D("Invest");
		$invest_array = $invest->get_invest_list($numSelect,$typeSelect,$markSelect);

		$json = json_encode($invest_array);
		$this->assign('invest_json',$json);

		$this->display();
	}


	/**
	 * 投资管理列表页#####放标动作
	 */
	public function invest_open() 
	{
		if ($_POST && array_key_exists('openData', $_POST) && $_POST['openData']) 
		{
			$data = $_POST['openData'];
			// echo "收到post请求：放标".$data;
			$model = D("Invest");
			$model->invest_status_id = '2';
			if ($model->where('invest_id='.$data)->save()) 
			{
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 * 投资管理列表页#####取消动作
	 */
	public function invest_cancel() 
	{
		if ($_POST && array_key_exists('cancelData', $_POST) && $_POST['cancelData']) 
		{
			$data = $_POST['cancelData'];
			// echo "收到post请求：取消".$data;
			$model = D("Invest");
			$model->invest_status_id = '3';
			if ($model->where('invest_id='.$data)->save()) 
			{
				$borrow_info = D('Borrowinfo');
				$borrow_info->borrow_investId = '0';
				$borrow_info->where('borrow_investId='.$data)->save();
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 * 投资新增
	 */
	public function invest_add() 
	{
		$type = D("InvestType");
		$type_array = $type->select();
		$this->assign('type_array',$type_array);

		if ($_POST) 
		{
			$insert = D('InvestProject');
			$data['invest_title'] = $_POST['investTitle'];		//	标题
			$data['invest_type_id'] = $_POST['investTypeId'];	//	投资类型ID
			$endTime = date("Y-m-d", strtotime("+" . $_POST['timeLimit'] . " months", strtotime(date("Y-m-d"))));
			$investTimeLimit = round((strtotime($endTime) - strtotime(date("Y-m-d")))/3600/24);
			$data['invest_time_limit'] = $investTimeLimit;		//	投资期限
			$data['remark'] = $_POST['remarks'];				//	备注
			$data['invest_status_id'] = '4';					//	投资状态ID
			$data['create_time'] = date('Y-m-d H:i:s', time());	//	创建时间
			if ($data['invest_type_id'] == 1) 
			{
				$data['invest_total_amount'] = $_POST['idMoneySum'];	//	总金额
				$data['invest_rate'] = $_POST['invest_rate'];			//	投资利率
				$data['active_time_id'] = $_POST['activeTime'];			//	有效时间ID
				$data['loan_grade'] = $_POST['loanGrade'];				//	借款等级
				$data['loan_timeLimit'] = $_POST['loanPeriod'];			//	借款期数
				$data['contract_id'] = $this->guid();					//	合同ID
			}
			elseif ($data['invest_type_id'] == 3) 
			{
				$data['invest_total_amount'] = $_POST['luckyStarTotalAmount'];		//	总金额
				$data['invest_number'] = $_POST['luckyStarTotalCount'];				//	总人数
				$data['invest_rate'] = $_POST['luckyStar_rate'];					//	收益率
			}

			if ($result = $insert->add($data)) 
			{
				if ($data['invest_type_id'] == 1) 
				{
					$update = D('Borrowinfo');
					$idArray = explode(',', $_POST['idArray']);
					for ($i=0; $i < count($idArray); $i++) 
					{
						$update->borrow_investId = $result;
						$update->where('borrow_bid='.$idArray[$i])->save();
					}
				}
				echo "<script>alert('新增投资项目成功！');history.back();</script>";
			}
			else
			{
				echo "<script>alert('新增投资项目失败！');history.back();</script>";
			}
			exit;
		}
		
		$this->display();
	}


	/**
	 * 投资新增#####生成合同ID
	 */
	public function guid() 
	{
		if (function_exists('com_create_guid')) 
		{
			return com_create_guid();
		}
		else
		{
			mt_srand((double)microtime()*10000);
			$charid = strtoupper(md5(uniqid(rand(), true)));
			$hyphen = chr(45);
			$uuid = substr($charid, 0, 8) . $hyphen
					.substr($charid, 8, 4) . $hyphen
					.substr($charid,12, 4) . $hyphen
					.substr($charid,16, 4) . $hyphen
					.substr($charid,20,12);
			return $uuid;
		}
	}


	/**
	 * 投资新增#####根据选择的投资类型给出不同的数据
	 */
	public function invest_add_annex() 
	{
		$borrowDate = D("BorrowDate");
		$borrowDate_array = $borrowDate->select();

		$InvestActivetime = D("InvestActivetime");
		$InvestActivetime_array = $InvestActivetime->select();

		$typeId = $_POST["typeId"];
		echo '<script type="text/javascript" src="__PUBLIC__/js/easyuiTab/jquery-1.4.4.min.js"></script>';
		echo '<script type="text/javascript" src="__PUBLIC__/js/easyuiTab/jquery.easyui.min.js"></script>';
		echo '<script type="text/javascript" src="__PUBLIC__/js/ajax.js"></script>';
		echo "<table class='addForm'>";
		echo "<input type='hidden' id='idArray' name='idArray' value='0'/>";
		echo "<input type='hidden' id='idMoneySum' name='idMoneySum' value='0'/>";
		if($typeId == 1) 
		{
			echo "<tr>";
			echo "<th><span></span>借款等级：</th>";
			echo "<td>";
			echo "<select name='loanGrade' id='loanGrade' onchange='ajaxToBorrow(0);'>";
			echo "<option value=''>请选择</option>";
			echo "<option value='A'>A</option>";
			echo "<option value='B'>B</option>";
			echo "<option value='C'>C</option>";
			echo "<option value='D'>D</option>";
			echo "<option value='E'>E</option>";
			echo "</select>";
			echo "<span id='loanGradeSpan'></span>";
			echo "</td>";
			echo "</tr>";

			echo "<tr>";
			echo "<th><span></span>投资利率：</th>";
			echo "<td>";
			echo "<input type='text' name='invest_rate' id='invest_rate' />";
			echo "<span id='loanGradeSpan'></span>";
			echo "</td>";
			echo "</tr>";

			echo "<tr>";
			echo "<th><span></span>借款期数：</th>";
			echo "<td>";
			echo "<select name='loanPeriod' id='loanPeriod' onchange='ajaxToBorrow(1);'>";
			echo "<option value=''>请选择</option>";
			foreach ($borrowDate_array as $key => $value) 
			{
				echo '<option value="'.$value['dateno'].'">'.$value['dateslip'].'</option>';
			}
			echo "</select>";
			echo "<span id='loanPeriodSpan'></span>";
			echo "</td>";
			echo "</tr>";

			echo "<tr>";
			echo "<th><span>*</span>有效时间：</th>";
			echo "<td>";
			echo "<select name='activeTime' id='activeTime'>";
			echo "<option value=''>请选择</option>";
			foreach ($InvestActivetime_array as $key => $value) 
			{
				echo '<option value="'.$value['active_time_id'].'">'.$value['active_time'].'天</option>';
			}
			echo "</select>";
			echo "<span id='activeTimeSpan'></span>";
			echo "</td>";
			echo "</tr>";
			
			echo "<tr>";
			echo "<th><span>*</span>投资期限：</th>";
			echo "<td>";
			echo "<select name='timeLimit' id='timeLimit' onchange='timeLimitChange();'>";
			echo "<option value=''>请选择</option>";
			foreach ($borrowDate_array as $key => $value) 
			{
				echo '<option value="'.$value['dateno'].'">'.$value['dateslip'].'</option>';
			}
			echo "</select>";
			echo "<span id='timeLimitSpan'></span>";
			echo "</td>";
			echo "</tr>";	
		}
		elseif($typeId == 3) 
		{
			echo "<tr>";
			echo "<th><span>*</span>总金额：</th>";
			echo "<td>";
			echo "<input type='text' name='luckyStarTotalAmount' id='luckyStarTotalAmount' />";
			echo "<span id='luckyStarTotalAmountSpan'></span>";
			echo "</td>";
			echo "</tr>";

			echo "<tr>";
			echo "<th><span>*</span>总人数：</th>";
			echo "<td>";
			echo "<input type='text' name='luckyStarTotalCount' id='luckyStarTotalCount' />";
			echo "<span id='luckyStarTotalCountSpan'></span>";
			echo "</td>";
			echo "</tr>";

			echo "<tr>";
			echo "<th><span>*</span>收益率：</th>";
			echo "<td>";
			echo "<input type='text' name='luckyStar_rate' id='luckyStar_rate' />";
			echo "<span id='luckyStarSpan'></span>";
			echo "</td>";
			echo "</tr>";

			echo "<tr>";
			echo "<th><span>*</span>投资期限：</th>";
			echo "<td>";
			echo "<select name='timeLimit' id='timeLimit' onchange='timeLimitChange();'>";
			echo "<option value=''>请选择</option>";
			foreach ($borrowDate_array as $key => $value) 
			{
				echo '<option value="'.$value['dateno'].'">'.$value['dateslip'].'</option>';
			}
			echo "</select>";
			echo "<span id='timeLimitSpan'></span>";
			echo "</td>";
			echo "</tr>";
		}
		echo "<tr>";
		echo "<th><span></span>备注：</th>";
		echo "<td>";
		echo "<textarea rows='5' cols='10' id='remarks' name='remarks'></textarea>";
		echo "<span id='remarksSpan'></span>";
		echo "</td>";
		echo "</tr>";

		echo "<tr>";
		echo '<td colspan="2" class="addButton">';
		echo '<input type="hidden" id="loadNameFlag">';
		echo '<input type="button" value="保存" class="save" onclick="investSubmit();">';
		echo '<input type="reset" value="重置" class="clear"></td>';
		echo '</tr>';
		echo "</table>";
		echo "<div id='borrowDetail'>";
		echo "</div>";
	}


	/**
	 * 投资新增#####根据选择的投资类型给出不同的数据
	 */
	public function invest_borrow_annex() 
	{
		$borrowBuinessinfo = D('BorrowBuinessinfo');
		$borrowBuinessinfo_array = $borrowBuinessinfo->get_invest_detail(I("post.loanGrade",""),I("post.loanPeriod",""));

		$json = json_encode($borrowBuinessinfo_array);
		$this->assign('borrowBuinessinfo_array',$json);

		$this->display();
	}


	/**
	 * 投资详情查看
	 */
	public function invest_see() 
	{
		if (IS_GET && I('get.invest_id')) 
		{
			// echo "get到了参数invest_id：<br>".I('get.invest_id')."<br>";
			$data = D("Invest");
			$data_array = $data->get_invest_detail(I('get.invest_id'));
			$this->assign('data_array',$data_array);

			$luck = D("InvestLuckly");
			$condition->invest_id = I('get.invest_id');
			$luck_array = $luck->where($condition)->select();
			$this->assign('luck_array',$luck_array[0]);

			$detail = D("InvestDetail");
			$detail_array = $detail->get_invest_detail(I('get.invest_id'));
			$json = json_encode($detail_array);
			$this->assign('detail_json',$json);
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
		$this->display();
	}


	/**
	 * 投资详情查看#####明细列表数据
	 */
	public function profit_detail_annex() 
	{
		$invest_id = I("post.invest_id","");
		$customer_id = I("post.customer_id","");

		$data2 = D("InvestProfit");
		$data_array2 = $data2->get_invest_detail($invest_id,$customer_id);
		$json2 = json_encode($data_array2);
		$this->assign('data_array2',$json2);

		$this->display();
	}


	/**
	 * 投资详情查看#####新增订单
	 */
	public function invest_order_add() 
	{
		if (IS_GET && I('get.invest_id')) 
		{
			$data = D("Invest");
			$conditions->invest_id = I('get.invest_id');
			$data_array = $data->where($conditions)->select();
			$this->assign('data_array',$data_array[0]);

			$orderList = D("InvestOrderlucky");
			$condition->invest_id = I('get.invest_id'); 
			$condition->order_mark = '1';
			$orderList_array = $orderList->where($condition)->select();
			foreach ($orderList_array as $key => $value) 
			{
				$orderList_array[$key]['invest_title'] = $data_array[0]['invest_title'];
			}
			$json = json_encode($orderList_array);
			$this->assign('orderList_json',$json);
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}

		$this->display();
	}


	/**
	 * 投资详情查看#####新增订单提交
	 */
	public function invest_order_add_submit() 
	{
		if ($_POST) 
		{
			$insert = D('InvestOrderlucky');
			$data['order_num'] = $_POST['invest_borrowNum'];			//	借款编号
			$data['invest_id'] = $_POST['invest_id'];					//	所属项目ID
			$data['order_borrowName'] = $_POST['invest_borrowName'];	//	借款人姓名
			$data['order_cardNum'] = $_POST['invest_borrowCardNum'];	//	借款人身份证
			$data['order_borrowMoney'] = $_POST['invest_borrowMoney'];	//	借款金额
			$data['order_period'] = $_POST['invest_borrowPeriod'];		//	借款期数
			$data['order_borrowTime'] = $_POST['invest_borrowDate'];	//	借款时间
			$data['order_serviceType'] = $_POST['invest_borrowType'];	//	服务类型

			if ($insert->add($data)) 
			{
				echo "<script>alert('新增订单成功！');window.location.href='../../invest/invest/invest_order_add?invest_id=".$_POST['invest_id']."';</script>";
			}
			else
			{
				echo "<script>alert('新增订单失败！');window.location.href='../../invest/invest/invest_order_add?invest_id=".$_POST['invest_id']."';</script>";
			}
			exit;
		}
	}


	/**
	 * 投资详情查看#####订单列表删除
	 */
	public function invest_order_add_del() 
	{
		if ($_POST && array_key_exists('cancelData', $_POST) && $_POST['cancelData']) 
		{
			$data = $_POST['cancelData'];
			// echo "收到post请求：取消".$data;
			$model = D("InvestOrderlucky");
			$model->order_mark = '0';
			if ($model->where('order_id='.$data)->save()) 
			{
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 * 投资详情查看#####前往抽奖
	 */
	public function invest_luckly() 
	{
		if (IS_GET && I('get.invest_id')) 
		{
			$data = D("Invest");
			$conditions->invest_id = I('get.invest_id');
			$data_array = $data->where($conditions)->select();
			$this->assign('data_array',$data_array[0]);

			$investDetail = D("InvestDetail");
			$condition->invest_id = I('get.invest_id');
			$investDetail_array = $investDetail->where($condition)->select();
			$count = count($investDetail_array);
			$this->assign('count',$count);
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}

		$this->display();
	}


	/**
	 * 投资详情查看#####前往抽奖提交
	 */
	public function invest_luckly_submit() 
	{
		if ($_POST && array_key_exists('invest_id', $_POST) && $_POST['invest_id']) 
		{
			$insert = D('InvestLuckly');
			$data['invest_id'] = $_POST['invest_id'];
			$data['luckly_number'] = $_POST['luckly_number'];					//	抽奖数字
			$data['invest_number'] = $_POST['luckly_number'] % $_POST['count'];	//	中奖序号
			$data['luckly_time'] = $_POST['luckly_time'];						//	上证股票时间
			$data['create_time'] = date('Y-m-d H:i:s', time());					//	创建时间

			if ($insert->add($data)) 
			{
				echo "<script>alert('中奖者为序号：".$data['invest_number']."');window.location.href='../../invest/invest/invest_see?invest_id=".$data['invest_id']."';</script>";
			}
			else
			{
				echo "<script>alert('新增订单失败！');window.location.href='../../invest/invest/invest_see?invest_id=".$data['invest_id']."';</script>";
			}
			exit;
		}
	}

}